//获取应用实例
const app = getApp ();

Page ({
    data: {
        now_page: 0,
        list_total: 0,
        color_list: [],
        sel_list: [],
        purchase_table_head: [],
        template_id: 0,
        keywords: '',
        limit: 40,
        table_scroll_left: 0,
        sel_color_list: []
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout (function () {
            __this.setData ({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },
    onLoad: function () {
        // 设置标题
        wx.setNavigationBarTitle ({
            title: '选择货品颜色'
        });
        // 接收传递过来的数据
        var __this = this;
        var eventChannel = this.getOpenerEventChannel ();
        // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
        eventChannel.on ('acceptDataFromOpenerPage', function (data) {
            console.log (data)
            data.sel_color_list.forEach (function (item, index) {
                data.sel_color_list[index].num = '';
                data.sel_color_list[index].skc = item.specAttributeValueId;
                if ((__this.data.sel_list.findIndex (obj => obj.specAttributeValueId === item.specAttributeValueId)) > -1) {
                    data.sel_color_list[index].is_checked = 1;
                }
            });
            __this.setData ({
                sel_list: data.sel_list,
                purchase_table_head: data.purchase_table_head,
                template_id: data.template_id,
                sel_color_list: data.sel_color_list
            })
        });
    },

    // 页面触底时执行
    onReachBottom: function () {
        // this.get_color_list();
    },

    // 获取颜色

    // 选择颜色时
    colorChange: function (e) {
        console.log (this.data.sel_color_list)
        var index = e.currentTarget.dataset.index;
        var choose_index = this.data.sel_list.findIndex (item => item.specAttributeValueId === this.data.sel_color_list[index].specAttributeValueId);
        if (e.detail.value.length) {
            if (choose_index >= 0) {
                wx.showToast ({
                    title: '已经选择过此颜色了',
                    icon: 'none',
                    duration: 1500,
                });
                return false;
            }
            // 选中
            this.data.sel_list.push (this.data.sel_color_list[index]);
            this.data.sel_color_list[index].is_checked = 1;
        } else {
            // 取消选中
            if (choose_index >= 0) {
                this.data.sel_list.splice (choose_index, 1);
                this.data.sel_color_list[index].is_checked = 0;
            }
        }
        this.setData ({
            sel_list: this.data.sel_list,
            sel_color_list: this.data.sel_color_list
        });
    },

    // 搜索
    GoSearch: function (event) {
        var keywords = event.detail.value.trim ();
        this.setData ({
            keywords: keywords,
            color_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_color_list (1);
    },

    // 取消选择
    chooseDel: function (e) {
        var index = e.currentTarget.dataset.index;
        console.log ('indexindexindexindexindex', index)
        // 将此条数据删除并将对应的取消选择
        var choose_index = this.data.color_list.findIndex (obj => obj.id === this.data.sel_list[index].id);
        if (choose_index > -1) {
            this.data.color_list[choose_index].is_checked = 0;
        }
        this.data.sel_list.splice (index, 1);
        // 重置data
        this.setData ({
            sel_list: this.data.sel_list,
            color_list: this.data.color_list
        });
    },

    // 当点击确定选择时
    chooseConfirm: function (e) {
        var __this = this;
        // 向上一级页面传递参数
        var eventChannel = __this.getOpenerEventChannel ();
        eventChannel.emit ('changeData', __this.data.sel_list);
        // 返回上一页
        wx.navigateBack ();
    },

    // 输入生产数量时
    selInputChange: function (e) {
        var index = e.currentTarget.dataset.index;
        var field = e.currentTarget.dataset.field;
        this.data.sel_list[index][field] = e.detail.value;
        this.setData ({
            sel_list: this.data.sel_list
        })
    },
    // 采购时间
    PurchaseBindDateChange: function (e) {
        let index = e.currentTarget.dataset.index
        var bianl = `sel_list[${index}].expectReturnDate`;
        this.setData ({
            [bianl]: e.detail.value
        })
    },
    // 采购时间
    bindDateChange: function (e) {
        let index = e.currentTarget.dataset.index
        this.data.sel_list[index].expectReturnDate = e.detail.value;
        this.setData ({
            sel_list: this.data.sel_list
        })
    },


})
